#ifndef PROTOCOL_H
#define PROTOCOL_H

#include "CtrlBoxGlobal.h"

typedef unsigned int uint;

enum SMPFault
{
                                        //bit0 ：预留
    Lvdt0_CollectErr    = 0x2,          //bit1 ：Lvdt0采集有故障
    Lvdt0_TooLow        = 0x4,          //bit2 ：Lvdt0采集值低于下限
    Lvdt0_TooHeight     = 0x8,          //bit3 ：Lvdt0采集值高于上限
    Lvdt0_Va_TooLow     = 0x10,         //bit4 ：Lvdt0_Va采集值低于下限
    Lvdt0_Va_TooHeight  = 0x20,         //bit5 ：Lvdt0_Va采集值高于上限
    Lvdt0_Vb_TooLow     = 0x40,         //bit6 ：Lvdt0_Vb采集值低于下限
    Lvdt0_Vb_TooHeight  = 0x80,         //bit7 ：Lvdt0_Vb采集值高于上限
                                        //bit8 ：预留
    Lvdt1CollectErr     = 0x200,        //bit9 ：Lvdt1采集有故障
    Lvdt1_TooLow        = 0x400,        //bitA ：Lvdt1采集值低于下限
    Lvdt1_TooHeight     = 0x800,        //bitB ：Lvdt1采集值高于上限
    Lvdt1_Va_TooLow     = 0x1000,       //bitC ：Lvdt1_Va采集值低于下限
    Lvdt1_Va_TooHeight  = 0x2000,       //bitD ：Lvdt1_Va采集值高于上限
    Lvdt1_Vb_TooLow     = 0x4000,       //bitE ：Lvdt1_Vb采集值低于下限
    Lvdt1_Vb_TooHeight  = 0x8000,       //bitF ：Lvdt1_Vb采集值高于上限
};

enum BITFault
{
    CPU_Err             = 0x1,          //bit0 ：CPU故障
    RAM_Err             = 0x2,          //bit1 ：RAM故障
                                        //bit2 ：预留
    Arg_Err             = 0x8,          //bit3 ：可调参数故障
    Lvdt0_CalErr        = 0x10,         //bit4 ：Lvdt0标定曲线故障
    Lvdt1_CalErr        = 0x20,         //bit5 ：Lvdt1标定曲线故障
    Lvdt0_ExcitErr      = 0x40,         //bit6 ：Lvdt0激励电压采集故障
    Lvdt1_ExcitErr      = 0x80,         //bit7 ：Lvdt1激励电压采集故障
    Voltage27_Err       = 0x100,        //bit8 ：27V电源电压采集故障
    Voltage_15_A_Err    = 0x200,        //bit9 ：15V电压A采集故障
    Voltage_15_B_Err    = 0x400,        //bitA ：15V电压B采集故障
    Voltage_3_3_Err     = 0x800,        //bitB ：3.3V电压采集故障
    Power_1_Err         = 0x1000,       //bitC ：稳压电源1采集故障
    Power_2_Err         = 0x2000,       //bitD ：稳压电源2采集故障
    Power_3_Err         = 0x4000,       //bitE ：稳压电源3采集故障
                                        //bitF ：预留
};


struct BoxStatus
{
    uint Head;              //帧数据头，固定为：0xAAAAAAAA
    uint Cmd;               //命令字：0x00003333
    uint DataLen;           //数据长度：总帧的字节长度（128字节，每个数据4字节，共32个数据，128字节）
    uint PeriodCnt;         //周期计数器：软件的5ms周期计数
    uint State;             //状态字: 0->初始化状态;0x00000008->工作状态
    uint Reserve1;          //预留1
    uint FaultWord_SMP;     //故障码
    uint FaultWord_BIT;     //故障码
    uint FaultWord_Reserve; //预留故障码
    uint Reserve2;          //预留2
    float Ratio_Lvdt0;      //Lvdt0差和比值
    float Lvdt0;            //Lvdt0物理值
    float Lvdt0_Va;         //Lvdt0_Va电压
    float Lvdt0_Vb;         //Lvdt0_Vb电压
    float Ratio_Lvdt1;      //Lvdt1差和比值
    float Lvdt1;            //Lvdt1物理值
    float Lvdt1_Va;         //Lvdt1_Va电压
    float Lvdt1_Vb;         //Lvdt1_Vb电压
    float FreqLvdt0Exci;    //Lvdt0激励输出频率
    float FreqLvdt1Exci;    //Lvdt1激励输出频率
    float VoltageLvdt0Exci; //Lvdt0激励回采电压
    float VoltageLvdt1Exci; //Lvdt1激励回采电压
    float Voltage27V;       //27V电源电压
    float Voltage15V_A;     //板上15V A电压
    float Voltage15V_B;     //板上15V B电压
    float Voltage3d3V;      //板上3.3V电压
    float VoltageConstant1; //稳压电源1
    float VoltageConstant2; //稳压电源2
    float VoltageConstant3; //稳压电源3
    float Reserve9;         //预留9
    float SoftVersion;      //软件版本号：0x07020100
    float CheckSum;         //累加校验和
};

extern "C" {

//采用UDP打开服务端
CTRLBOX_EXPORT bool openHost( const char* ipAddr = "192.168.1.101", int port = 1801);
//关闭服务端
CTRLBOX_EXPORT void closeHost();

//读取周期通信协议（单次），发送指令 03 16 19,然后读入数据
CTRLBOX_EXPORT BoxStatus readSingleStatus( bool *ok = nullptr );

//设置连续通讯协议（周期），发送指令 03 17 1A，
CTRLBOX_EXPORT bool setContinuousCMD();

//停止周期通信协议, 发送指令 03 37 3A
CTRLBOX_EXPORT bool stopContinuousCMD();

//读取标定数据
CTRLBOX_EXPORT void readCalibration();


//读一次协议内容
CTRLBOX_EXPORT BoxStatus readBoxStatus(bool *ok = nullptr );

//读取Lvdt0标定值
CTRLBOX_EXPORT bool readLvdt0CalibratData(float* data, unsigned int size);
//读取Lvdt1标定值
CTRLBOX_EXPORT bool readLvdt1CalibratData(float* data, unsigned int size);

CTRLBOX_EXPORT int testAdd( int a, int b );

}

#endif // PROTOCOL_H
